"""
迭代器练习
"""

# 迭代器
class StudentItertor:
    def __init__(self, data):
        self.data = data
        self.index = -1



    def __next__(self):
        self.index += 1
        if self.index < len(self.data):
            return self.data[self.index]
        raise StopIteration

# 可迭代对象
class StudentController:
    def __init__(self):
         self.list_student = []

    def __iter__(self):
        return StudentItertor(self.list_student)



# 创建一个迭代器对象 存放如干学生信息
s1 = StudentController()
print(s1.__dict__) #{'list_student': []}
s1.list_student.append("聂茹凤")
s1.list_student.append("谭新宇")
s1.list_student.append("韩耀祖")
s1.list_student.append("李欣桐")
s1.list_student.append("刘千琪")
print(s1.__dict__) # {'list_student': ['聂茹凤', '谭新宇', '韩耀祖', '李欣桐', '刘千琪']}

#  迭代此迭代器对象
iterator = s1.__iter__()
while True:
    try:
        item = iterator.__next__()
        print(item)
    except StopIteration:
        break